package com.lx.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lx.biz.IDept;
import com.lx.biz.IEmploee;
import com.lx.domain.Dept;
import com.lx.domain.Emploee;
import com.lx.domain.User;;

@Controller
public class EmploeeCon {
	@Resource
	private IEmploee iEmploeeimpl ;
	@Resource
	private IDept iDeptimpl;
	
	@RequestMapping("emploee/emploeelist")
	public String getEmplist(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("user") ;
		if (user==null) {
			return "redirect:../login";	
		}
		else if(user.getRole() > 0) {
		List<Dept> deptlist=iDeptimpl.geteDepts("");
		request.setAttribute("user",user); 
		request.setAttribute("deptlist",deptlist);  
		return "emploee/emploeelist" ;
		}else {
			return "redirect:../login";
		}
	}
	
		
	@RequestMapping(value="emploee/emploee_edit")  
	public String getAllEmploee(String name,String dept_id, HttpServletRequest request ){ 
		User user = (User)request.getSession().getAttribute("user") ;
		if (user==null) {
			return "redirect:../login";	
		}
		else if (user.getRole() > 0) {
			List<Emploee> emploeelist =iEmploeeimpl.geteEmploees(name, dept_id);
			List<Dept> deptlist=iDeptimpl.geteDepts("");
		    request.setAttribute("emploeelist",emploeelist);  
		    request.setAttribute("deptlist",deptlist);  
		    return "emploee/emploee_edit";			
		}else {
			return "redirect:../login";	
		}
  
	}  

	@RequestMapping(value="emploee/emploee_save")  
	public String findEmploee(String id, HttpServletRequest request ){  
		Emploee emploee =iEmploeeimpl.findEmploee(id);
		request.setAttribute("emploee",emploee);  
	    return "emploee/emploee_save";  
	}  
	
	@RequestMapping(value="emploee/emploee_update")     //返回String类型字符串到前端而不是路径需加@ResponseBody注解
	public @ResponseBody String updateEmploee(Emploee emploee, HttpServletRequest request ){  
		iEmploeeimpl.update(emploee);
		return "success" ;
	}  
	
	@RequestMapping(value="emploee/emploee_edit_ord")    
	public @ResponseBody String edit_ord(int id1,int id2, HttpServletRequest request ){ 
		Emploee emploee1 =iEmploeeimpl.findEmploee(Integer.toString(id1)) ;
		Emploee emploee2 =iEmploeeimpl.findEmploee(Integer.toString(id2)) ;
		
		if (emploee1.getDept().getId()==emploee2.getDept().getId()) {
			iEmploeeimpl.edit_ord(emploee1,emploee2);
			return "success";
			
		}
		else {
			return "faile" ;
		}
	
	}  
	
	
	@RequestMapping(value="emploee/emploee_del")     //返回String类型字符串到前端而不是路径需加@ResponseBody注解
	public @ResponseBody String depEmploee(String ids, HttpServletRequest request ){
		
		iEmploeeimpl.delete(ids);
		return "success" ;
	}  
}
